import { NSelect } from "naive-ui";
import { getSlotsDom } from "../utils";
import type { HTMLAttributes } from "vue";
import type { SelectProps, SelectInst } from "naive-ui";
import type { BaseComponentType } from "../types";

export type SelectFactoryConstructorType = {
  props?: SelectProps;
  attrs?: HTMLAttributes;
  headerSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  emptySlot?: BaseComponentType;
  arrowSlot?: BaseComponentType;
};
export class SelectFactory {
  private props: SelectProps = {};
  private attrs: HTMLAttributes = {};
  private headerSlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;
  private emptySlot: BaseComponentType = null;
  private arrowSlot: BaseComponentType = null;
  private domRef: SelectInst | null = null;

  constructor(data?: SelectFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.headerSlot) this.setHeader(data.headerSlot);
    if (data?.actionSlot) this.setAction(data.actionSlot);
    if (data?.emptySlot) this.setEmpty(data.emptySlot);
    if (data?.arrowSlot) this.setArrow(data.arrowSlot);
  }

  setProps(props: SelectProps) {
    this.props = props;
    return this;
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }

  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }

  setEmpty(component: BaseComponentType) {
    this.emptySlot = component;
    return this;
  }

  setArrow(component: BaseComponentType) {
    this.arrowSlot = component;
    return this;
  }

  focus() {
    this.domRef?.focus();
  }
  focusInput() {
    this.domRef?.focusInput();
  }
  blur() {
    this.domRef?.blur();
  }
  blurInput() {
    this.domRef?.blurInput();
  }

  create() {
    return (
      <NSelect
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as SelectInst;
        }}
        {...this.props}
        {...this.attrs}
      >
        {{
          header: () => getSlotsDom(this.headerSlot),
          action: () => getSlotsDom(this.actionSlot),
          empty: () => getSlotsDom(this.emptySlot),
          arrow: () => getSlotsDom(this.arrowSlot),
        }}
      </NSelect>
    );
  }
}
